home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / src / mosmlyac / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-18  |  7.3 KB  |  394 lines  |  [TEXT/R*ch]

  1. #include <signal.h>
  2. #include "defs.h"
  3.  
  4. char dflag;
  5. char lflag;
  6. char rflag;
  7. char tflag;
  8. char vflag;
  9. char big_endian;
  10.  
  11. char *file_prefix = 0;
  12. char *myname = "yacc";
  13. #ifdef NO_UNIX
  14. char temp_form[] = "yacc.X";
  15. #else
  16. char temp_form[] = "yacc.XXXXXXX";
  17. #endif
  18.  
  19. int lineno;
  20. int outline;
  21.  
  22. char *action_file_name;
  23. char *entry_file_name;
  24. char *code_file_name;
  25. char *interface_file_name;
  26. char *defines_file_name;
  27. char *input_file_name = "";
  28. char *output_file_name;
  29. char *text_file_name;
  30. char *union_file_name;
  31. char *verbose_file_name;
  32.  
  33. FILE *action_file;    /*  a temp file, used to save actions associated    */
  34.             /*  with rules until the parser is written        */
  35. FILE *entry_file;
  36. FILE *code_file;    /*  y.code.c (used when the -r option is specified) */
  37. FILE *defines_file;    /*  y.tab.h                        */
  38. FILE *input_file;    /*  the input file                    */
  39. FILE *output_file;    /*  y.tab.c                        */
  40. FILE *text_file;    /*  a temp file, used to save text until all        */
  41.             /*  symbols have been defined                */
  42. FILE *union_file;    /*  a temp file, used to save the union            */
  43.             /*  definition until all symbol have been        */
  44.             /*  defined                        */
  45. FILE *verbose_file;    /*  y.output                        */
  46. FILE *interface_file;
  47.  
  48. int nitems;
  49. int nrules;
  50. int ntotalrules;
  51. int nsyms;
  52. int ntokens;
  53. int nvars;
  54.  
  55. int   start_symbol;
  56. char  **symbol_name;
  57. short *symbol_value;
  58. short *symbol_prec;
  59. char  *symbol_assoc;
  60. char **symbol_tag;
  61. char *symbol_true_token;
  62.  
  63. short *ritem;
  64. short *rlhs;
  65. short *rrhs;
  66. short *rprec;
  67. char  *rassoc;
  68. short **derives;
  69. char *nullable;
  70.  
  71. extern char *mktemp();
  72. extern char *getenv();
  73.  
  74.  
  75. done(k)
  76. int k;
  77. {
  78.     if (action_file) { fclose(action_file); unlink(action_file_name); }
  79.     if (entry_file) { fclose(entry_file); unlink(entry_file_name); }
  80.     if (text_file) { fclose(text_file); unlink(text_file_name); }
  81.     if (union_file) { fclose(union_file); unlink(union_file_name); }
  82.     exit(k);
  83. }
  84.  
  85.  
  86. void onintr(dummy)
  87.      int dummy;
  88. {
  89.     done(1);
  90. }
  91.  
  92.  
  93. set_signals()
  94. {
  95. #ifdef SIGINT
  96.     if (signal(SIGINT, SIG_IGN) != SIG_IGN)
  97.     signal(SIGINT, onintr);
  98. #endif
  99. #ifdef SIGTERM
  100.     if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
  101.     signal(SIGTERM, onintr);
  102. #endif
  103. #ifdef SIGHUP
  104.     if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
  105.     signal(SIGHUP, onintr);
  106. #endif
  107. }
  108.  
  109.  
  110. usage()
  111. {
  112.     fprintf(stderr, "usage: %s [-v] [-b file_prefix] [-el|-eb] filename\n",
  113.             myname);
  114.     exit(1);
  115. }
  116.  
  117. sflag()
  118. {
  119.     fprintf(stderr, "%s: The -s flag is obsolete and has no effect.\n",
  120.         myname);
  121. }
  122.  
  123. getargs(argc, argv)
  124. int argc;
  125. char *argv[];
  126. {
  127.     register int i;
  128.     register char *s;
  129.  
  130.     if (argc > 0) myname = argv[0];
  131.     for (i = 1; i < argc; ++i)
  132.     {
  133.     s = argv[i];
  134.     if (*s != '-') break;
  135.     switch (*++s)
  136.     {
  137.     case '\0':
  138.         input_file = stdin;
  139.         if (i + 1 < argc) usage();
  140.         return;
  141.  
  142.     case '-':
  143.         ++i;
  144.         goto no_more_options;
  145.  
  146.     case 'b':
  147.         if (*++s)
  148.          file_prefix = s;
  149.         else if (++i < argc)
  150.         file_prefix = argv[i];
  151.         else
  152.         usage();
  153.         continue;
  154.  
  155.     case 'v':
  156.         vflag = 1;
  157.         break;
  158.  
  159.     case 's':
  160.         sflag();
  161.         break;
  162.  
  163.     default:
  164.         usage();
  165.     }
  166.  
  167.     for (;;)
  168.     {
  169.         switch (*++s)
  170.         {
  171.         case '\0':
  172.         goto end_of_option;
  173.  
  174.         case 'v':
  175.         vflag = 1;
  176.         break;
  177.  
  178.         case 's':
  179.         sflag();
  180.         break;
  181.  
  182.         default:
  183.         usage();
  184.         }
  185.     }
  186. end_of_option:;
  187.     }
  188.  
  189. no_more_options:;
  190.     if (i + 1 != argc) usage();
  191.     input_file_name = argv[i];
  192.     if (file_prefix == 0) {
  193.       int len;
  194.       len = strlen(argv[i]);
  195.       file_prefix = malloc(len + 1);
  196.       if (file_prefix == 0) no_space();
  197.       strcpy(file_prefix, argv[i]);
  198.       while (len > 0) {
  199.         len--;
  200.         if (file_prefix[len] == '.') {
  201.           file_prefix[len] = 0;
  202.           break;
  203.         }
  204.       }
  205.     }
  206. }
  207.  
  208.  
  209. char *
  210. allocate(n)
  211. unsigned n;
  212. {
  213.     register char *p;
  214.  
  215.     p = NULL;
  216.     if (n)
  217.     {
  218.     p = CALLOC(1, n);
  219.     if (!p) no_space();
  220.     }
  221.     return (p);
  222. }
  223.  
  224.  
  225. create_file_names()
  226. {
  227.     int i, len;
  228.     char *tmpdir;
  229.  
  230. #ifdef NO_UNIX
  231.     len = 0;
  232.     i = sizeof(temp_form);
  233. #else
  234.     tmpdir = getenv("TMPDIR");
  235.     if (tmpdir == 0) tmpdir = "/tmp";
  236.     len = strlen(tmpdir);
  237.     i = len + sizeof(temp_form);
  238.     if (len && tmpdir[len-1] != '/')
  239.     ++i;
  240. #endif
  241.  
  242.     action_file_name = MALLOC(i);
  243.     if (action_file_name == 0) no_space();
  244.     entry_file_name = MALLOC(i);
  245.     if (entry_file_name == 0) no_space();
  246.     text_file_name = MALLOC(i);
  247.     if (text_file_name == 0) no_space();
  248.     union_file_name = MALLOC(i);
  249.     if (union_file_name == 0) no_space();
  250.  
  251. #ifndef NO_UNIX
  252.     strcpy(action_file_name, tmpdir);
  253.     strcpy(entry_file_name, tmpdir);
  254.     strcpy(text_file_name, tmpdir);
  255.     strcpy(union_file_name, tmpdir);
  256.  
  257.     if (len && tmpdir[len - 1] != '/')
  258.     {
  259.     action_file_name[len] = '/';
  260.     entry_file_name[len] = '/';
  261.     text_file_name[len] = '/';
  262.     union_file_name[len] = '/';
  263.     ++len;
  264.     }
  265. #endif
  266.  
  267.     strcpy(action_file_name + len, temp_form);
  268.     strcpy(entry_file_name + len, temp_form);
  269.     strcpy(text_file_name + len, temp_form);
  270.     strcpy(union_file_name + len, temp_form);
  271.  
  272.     action_file_name[len + 5] = 'a';
  273.     entry_file_name[len + 5] = 'e';
  274.     text_file_name[len + 5] = 't';
  275.     union_file_name[len + 5] = 'u';
  276.  
  277. #ifndef NO_UNIX
  278.     mktemp(action_file_name);
  279.     mktemp(entry_file_name);
  280.     mktemp(text_file_name);
  281.     mktemp(union_file_name);
  282. #endif
  283.  
  284.     len = strlen(file_prefix);
  285.  
  286.     output_file_name = MALLOC(len + 7);
  287.     if (output_file_name == 0)
  288.     no_space();
  289.     strcpy(output_file_name, file_prefix);
  290.     strcpy(output_file_name + len, OUTPUT_SUFFIX);
  291.  
  292.     code_file_name = output_file_name;
  293.  
  294.     if (vflag)
  295.     {
  296.     verbose_file_name = MALLOC(len + 8);
  297.     if (verbose_file_name == 0)
  298.         no_space();
  299.     strcpy(verbose_file_name, file_prefix);
  300.     strcpy(verbose_file_name + len, VERBOSE_SUFFIX);
  301.     }
  302.  
  303.     interface_file_name = MALLOC(len + 8);
  304.     if (interface_file_name == 0)
  305.     no_space();
  306.     strcpy(interface_file_name, file_prefix);
  307.     strcpy(interface_file_name + len, INTERFACE_SUFFIX);
  308.  
  309. }
  310.  
  311.  
  312. open_files()
  313. {
  314.     create_file_names();
  315.  
  316.     if (input_file == 0)
  317.     {
  318.     input_file = fopen(input_file_name, "r");
  319.     if (input_file == 0)
  320.         open_error(input_file_name);
  321.     }
  322.  
  323.     action_file = fopen(action_file_name, "w");
  324.     if (action_file == 0)
  325.     open_error(action_file_name);
  326.  
  327.     entry_file = fopen(entry_file_name, "w");
  328.     if (entry_file == 0)
  329.     open_error(entry_file_name);
  330.  
  331.     text_file = fopen(text_file_name, "w");
  332.     if (text_file == 0)
  333.     open_error(text_file_name);
  334.  
  335.     if (vflag)
  336.     {
  337.     verbose_file = fopen(verbose_file_name, "w");
  338.     if (verbose_file == 0)
  339.         open_error(verbose_file_name);
  340.     }
  341.  
  342.     if (dflag)
  343.     {
  344.     defines_file = fopen(defines_file_name, "w");
  345.     if (defines_file == 0)
  346.         open_error(defines_file_name);
  347.     union_file = fopen(union_file_name, "w");
  348.     if (union_file ==  0)
  349.         open_error(union_file_name);
  350.     }
  351.  
  352.     output_file = fopen(output_file_name, "w");
  353.     if (output_file == 0)
  354.     open_error(output_file_name);
  355.  
  356.     if (rflag)
  357.     {
  358.     code_file = fopen(code_file_name, "w");
  359.     if (code_file == 0)
  360.         open_error(code_file_name);
  361.     }
  362.     else
  363.     code_file = output_file;
  364.  
  365.  
  366.     interface_file = fopen(interface_file_name, "w");
  367.     if (interface_file == 0)
  368.       open_error(interface_file_name);
  369. }
  370.  
  371. #if defined(THINK_C) || defined(__MWERKS__)
  372. #include <console.h>
  373. #endif
  374.  
  375. main(argc, argv)
  376. int argc;
  377. char *argv[];
  378. {
  379. #if defined(THINK_C) || defined(__MWERKS__)
  380.     argc = ccommand(&argv);
  381. #endif
  382.     set_signals();
  383.     getargs(argc, argv);
  384.     open_files();
  385.     reader();
  386.     lr0();
  387.     lalr();
  388.     make_parser();
  389.     verbose();
  390.     output();
  391.     done(0);
  392.     /*NOTREACHED*/
  393. }
  394.